{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[' this is a \\n                multiline comment ']\n"
     ]
    }
   ],
   "source": [
    "# 问题： 我们打算用正则表达式对一段文本做匹配，但是希望在进行匹配时能够跨越多行。\n",
    "# 解决方案 1.使用非捕获组(?:.|\\n) 2.re.DOTALL\n",
    "import re\n",
    "text = '''/* this is a \n",
    "                multiline comment */\n",
    "'''\n",
    "comment = re.compile(r'/\\*((?:.|\\n)*?)\\*/')\n",
    "m = comment.findall(text)\n",
    "print(m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[' this is a \\n                multiline comment ']\n"
     ]
    }
   ],
   "source": [
    "# 简单情况，使用re.DOTALL 标记。\n",
    "import re\n",
    "text = '''/* this is a \n",
    "                multiline comment */\n",
    "'''\n",
    "comment = re.compile(r'/\\*(.*?)\\*/',re.DOTALL)\n",
    "m = comment.findall(text)\n",
    "print(m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "python \n ok\n\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "text = '''/* this is a \n",
    "                multiline comment */\n",
    "'''\n",
    "comment = re.compile(r'/\\*(.*?)\\*/',re.DOTALL)\n",
    "m = comment.sub('python \\n ok',text)\n",
    "print(m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    ""
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2.0
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}